<div class="layuimini-container">
    <div class="layuimini-main">
		<div style="margin: 5px 5px 5px 5px">
			<form class="layui-form layui-form-pane" action="">
				<div class="layui-form-item">
					<div class="layui-inline">
						<div class="layui-input-inline">
							<input type="text" name="loginName" autocomplete="off" class="layui-input" placeholder="登录名">
						</div>
					</div>
					<input type="hidden" name="appId" id="appId"/>
					<div class="layui-inline">
						<button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
					</div>
				</div>
			</form>
		</div>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <!--<button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>-->
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
			{{#	if (d.modifyRights == 1){	}}
				<a href="javascript:;" class="layui-font-blue" lay-event="update">修改</a>
			{{# }			                	}}
			{{#	if (d.deleteRights == 1){	}}
				<span style="color: rgb(179, 179, 179);"> | </span>
				<a href="javascript:;" class="layui-font-blue" lay-event="delete">删除</a>
			{{# }			                	}}
        </script>
		
		<script type="text/html" id="roleTypeBar">
			{{#	var showRole = ''					}}
			{{#	for(var roleType of d.roleTypes){	}}
			{{#	if (roleType == 1){					}}
			{{#		showRole += ',管理员'			}}
			{{#	} else if (roleType == 2){			}}
			{{#		showRole += ',开发'				}}
			{{#	} else if (roleType == 3){			}}
			{{#		showRole += ',测试'				}}
			{{#	} else if (roleType == 4){			}}
			{{#		showRole += ',运维'				}}
			{{#	} else if (roleType == 5){			}}
			{{#		showRole += ',架构师'			}}
			{{#	} else if (roleType == 6){			}}
			{{#		showRole += ',告警接收'			}}
			{{#	} else if (roleType == 7){			}}
			{{#		showRole += ',部署审批'	}}
			{{# }			                }}
			{{# }			                }}
			{{#	if(showRole.startsWith(',')) {showRole = showRole.substring(1)}	}}
			{{#	return showRole				}}
		</script>
    </div>
</div>
<script>

	//全局变量，在子页面里使用
	var data;
	
    layui.use(['form', 'table', 'dropdown', 'miniPage'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
			dropdown = layui.dropdown,
			miniPage = layui.miniPage,
			appId = localStorage.getItem('appId_' + $("#userName").val());
		
		$("#appId").val(appId);
		
        table.render({
            elem: '#currentTableId',
            url: '/app/member/page',
			method: 'post',
			contentType: 'application/json;charset=UTF-8',
			request: {
				pageName: 'pageNum',
				limitName: 'pageSize'
			},
			where: {
				'appId': appId
			},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
			parseData: function(res){
				redirectLogin(res.code);
				if(res.code != '000000'){
					return {
					  "code": 1,
					  "msg": res.message,
					  "count": 0,
					  "data": []
					};
				}
				return {
				  "code": 0,
				  "msg": res.message,
				  "count": res.data.itemCount,
				  "data": res.data.items
				};
			},
            cols: [[
                {field: 'id', title: '编号'},
                {field: 'loginName', title: '登录名'},
				{field: 'userName', title: '用户名'},
                {field: 'roleType', title: '角色类型', templet: '#roleTypeBar'},
                {title: '操作', toolbar: '#currentTableBar'}
            ]],
            limits: [10, 20],
            limit: 10,
            page: true,
            skin: 'row',
			even: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: data.field
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加成员',
                    type: 2,
					shade: 0.5,
					maxmin: false,
					shadeClose: false,
					area: ['40%', '50%'],
					offset: ['23%', '30%'],
					content: ['page/app/member/add.html?random=' + Math.random(), 'no']
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            data = JSON.stringify(obj.data);
			if (obj.event === 'update') {
                var index = layer.open({
                    title: '修改成员',
                    type: 2,
					shade: 0.5,
					maxmin: false,
					shadeClose: false,
					area: ['40%', '50%'],
					offset: ['23%', '30%'],
					content: ['page/app/member/update.html', 'no']
                });
            }else if (obj.event === 'delete') {
                doDelete($, obj.data.appId, obj.data.loginName, obj.data.userName);
            }
			return false;
        });
    });
	
	function doDelete($, appId, loginName, userName) {
		var msg = '确定删除：' + userName + "？";
		layer.confirm(msg, function (index) {
			layer.close(index);
			$.ajax({
				url: '/app/member/delete',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify({"appId": appId, "loginName": loginName}),
				success: function(data){
					if(data.code != "000000"){
						layer.msg(data.message, {icon: 5, shift: 6});
						return false;
					}
					
					layer.msg("删除成功", {
						icon: 1,
						time: 500,
						shade: 0.01,
						shadeClose: false}, function(){
						layui.table.reload('currentTableId');
					});
				},
				error: function(data){
					layer.msg(data.message, {icon: 5, shift: 6});
				}
			});
		});
	}
</script>